#include "httphelper.h"
#include<QDebug>
#include<QJsonArray>
#include<QJsonDocument>
#include<QJsonValue>
#include <mutex>
#include<QJsonObject>
#include<QByteArray>
std::unique_ptr<HttpHelper>HttpHelper::instance_ =nullptr;
#define SERVER_URL "http://10.200.14.253:8083"




HttpHelper *HttpHelper::instance()
{
    static std::once_flag flag;
        std::call_once(flag,[&](){
        instance_.reset(new HttpHelper());
    });
      return instance_.get();
}

void HttpHelper::slot_GetmediaInfo()
{
    if(networkManager)
    {
        delete networkManager;
        networkManager=nullptr;
    }
}

void HttpHelper::slot_Register(const QVariantMap &infomap)
{
    QJsonObject paramsObj;
    paramsObj[""]=QJsonObject();
    QJsonObject rootObj;
    rootObj["createTime"]="";
    rootObj["updataTime"]="";
    rootObj["params"]=paramsObj;
    rootObj["userTd"]=0;
    rootObj["userName"]=infomap["userName"].toString();
    rootObj["nickName"]=infomap["nickName"].toString();
    rootObj["passwd"]=infomap["passwd"].toString();
    //qjsonobjeck转为jsondocument
    QJsonDocument doc(rootObj);
    QByteArray jsondata=doc.toJson(QJsonDocument::Compact);
    sendRequest(REGISTER,true,jsondata);

}

void HttpHelper::slot_Login(const QVariantMap &infomap)
{
    QJsonObject paramsObj;
    paramsObj[""] = QJsonObject();
    QJsonObject rootObj;
    rootObj["createTime"] ="";
    rootObj["updataTime"] = "";
    rootObj["params"] = paramsObj;
    rootObj["userId"] = 0;
    rootObj["userName"] = infomap["userName"].toString();
    rootObj["nickName"] = infomap["nickName"].toString();
    rootObj["passwd"] = infomap["passwd"].toString();

    QJsonDocument doc(rootObj);
    QByteArray jsondata = doc.toJson(QJsonDocument::Compact);
    sendRequest(LOGIN,true,jsondata);

}

//退出登录

void HttpHelper::slot_LoginOut(const QVariantMap &infomap)
{
    QJsonObject paramsObj;
      paramsObj[""] = QJsonObject();
      QJsonObject rootObj;
      QJsonDocument doc(rootObj);
      QByteArray jsondata = doc.toJson(QJsonDocument::Compact);
      sendRequest(LOGINOUT,true,jsondata);



}

void HttpHelper::slot_AddmediaInfo(const QVariantMap &infomap)
{
    QJsonObject paramsObj;
    paramsObj[""]=QJsonObject();
    QJsonObject rootObj;
    rootObj["createTime"]="";
    rootObj["updataTime"]="";
    rootObj["params"]=paramsObj;
    rootObj["mediaId"]=0;
    rootObj["userTd"]=0;
    rootObj["mediaUrl"]=infomap["mediaUrl"].toString();
    rootObj["mediaTitle"]=infomap["mediaTitle"].toString();
    rootObj["orderNum"]=0;
    //qjsonobjeck转为jsondocument
    QJsonDocument doc(rootObj);
    QByteArray jsondata=doc.toJson(QJsonDocument::Compact);
    sendRequest(ADDMEDIA,true,jsondata);

}


void HttpHelper::slot_EditmediaInfo(const QVariantMap &infomap)
{
    QJsonObject paramsObj;
    paramsObj[""]=QJsonObject();
    QJsonObject rootObj;
    rootObj["createTime"]="";
    rootObj["updataTime"]="";
    rootObj["params"]=paramsObj;
    rootObj["mediaId"]=0;
    rootObj["userTd"]=0;
    rootObj["mediaUrl"]=infomap["mediaUrl"].toString();
    rootObj["mediaTitle"]=infomap["mediaTitle"].toString();
    rootObj["orderNum"]=0;
    //qjsonobjeck转为jsondocument
    QJsonDocument doc(rootObj);
    QByteArray jsondata=doc.toJson(QJsonDocument::Compact);
    sendRequest(EDITMEDIA,true,jsondata);


}

void HttpHelper::slot_RemovemediaInfo(const QVariantMap &infomap)
{
    QJsonObject paramsObj;
    paramsObj[""]=QJsonObject();
    QJsonObject rootObj;
    rootObj["createTime"]="";
    rootObj["updataTime"]="";
    rootObj["params"]=paramsObj;
    rootObj["userTd"]=0;
    rootObj["userName"]=infomap["userName"].toString();
    rootObj["nickName"]=infomap["nickName"].toString();
    rootObj["passwd"]=infomap["passwd"].toString();
    //qjsonobjeck转为jsondocument
    QJsonDocument doc(rootObj);
    QByteArray jsondata=doc.toJson(QJsonDocument::Compact);
    sendRequest(REMOVEMEDIA,true,jsondata);

}

void HttpHelper::HandleReply(const QByteArray &data)
{
    QJsonParseError parseError;
    QJsonDocument jsonDoc=QJsonDocument::fromJson(data,&parseError);
    QByteArray jsonstring=jsonDoc.toJson(QJsonDocument::Indented);
    qDebug()<<"json data:"<<jsonstring.constData();
    //检查解析是否错误
    if(parseError.error!=QJsonParseError::NoError){
        qDebug()<<"JSON解析错误："<<parseError.errorString()<<"位置"<<parseError.offset;
        return;
    }
    //
    if(jsonDoc.isObject()){
        QJsonObject jsonObj=jsonDoc.object();
        bool operationSuccess=(jsonObj["code"].toInt()==200);
        int reponseType=jsonObj["responseType"].toInt();
        if(reponseType !=MEDIALIST)
        {
            if(reponseType==LOGIN&&operationSuccess){
                token =jsonObj["msg"].toString();

            }
            emit sig_Operator(static_cast<REQUESTTYPE>(reponseType),operationSuccess);
        }
        else if(reponseType==MEDIALIST && operationSuccess)
        {
            QJsonValue dataValue=jsonObj["data"];
            //检查data是否为数组
            QVector<QVariantMap>mediaList;
            if(dataValue.isArray())
            {
                QJsonArray dataArray=dataValue.toArray();
                //遍历数组提取里面的视频url和title
                for(const QJsonValue &itemValue :dataArray)
                {
                    if(itemValue.isArray())
                    {
                        QJsonObject itemObj=itemValue.toObject();
                        QVariantMap mediaItem;
                        if(itemObj.contains("mediaTitle")&&itemObj["mediaTitle"].isString())
                        {
                            mediaItem["mediaTitle"]=itemObj["mediaTitle"].toString();
                        }
                        if(itemObj.contains("mediaUrl")&&itemObj["mediaUrl"].isString())
                        {
                            mediaItem["mediaUrl"]=itemObj["mediaUrl"].toString();
                        }
                        mediaList.append(mediaItem);
                    }
                }
            }
            emit sig_MediaInfo(mediaList);

        }
        if(!operationSuccess)
        {
            qDebug()<<"操作失败："<<jsonObj["msg"].toString();
        }
    }


}

void HttpHelper::onReplyFinished(QNetworkReply *reply)
{
    //获取http状态码
    QVariant statusCode=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    //获取详细的错误信息
    QNetworkReply::NetworkError errorType=reply->error();
    QString errorMsg=reply->errorString();
    qDebug()<<"request ULR:"<<reply->url().toString();
    qDebug()<<"HTTP status code:"<<statusCode.toInt();
    qDebug()<<"Error Type:"<<errorType;
    qDebug()<<"Error Message:"<<errorMsg;

    QByteArray response=reply->readAll();
    qDebug()<<"response data:"<<QString::fromUtf8(response);

    if(errorType==QNetworkReply::NoError)
    {
        //处理应答
        HandleReply(response);
    }
    else {
        switch (errorType) {
        case QNetworkReply::ConnectionRefusedError:
            qDebug();
        case QNetworkReply::HostNotFoundError:
            qDebug()<<"主机未找到，IP或者DNS错误";
            break;
        case QNetworkReply::TimeoutError:
            qDebug()<<"请求超时";
            break;
        case QNetworkReply::ContentNotFoundError:
            qDebug()<<"连接没有找到，URL错误";
            break;
        default:
            qDebug()<<"其他错误"<<errorMsg;

        }
    }
    reply->deleteLater();

}

void HttpHelper::sendGetRequest(const QNetworkRequest &request)
{
    networkManager->get(request);

}

void HttpHelper::sendRostRequest(QNetworkRequest &request, const QByteArray &data)
{
    networkManager->post(request,data);

}

void HttpHelper::sendRequest(const REQUESTTYPE type, bool isPost, const QByteArray data)
{
    QUrl url=QUrl::fromUserInput(requestAdders[type]);
    if(!url.isValid()){
        qDebug()<<"url无效";
        return;
    }
    QNetworkRequest request(url);
    //指定数据为json
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
    //设置token
    QByteArray authHeader=token.toUtf8();
    request.setRawHeader("Authorization",authHeader);
    isPost ? sendRostRequest(request,data):sendGetRequest(request);

}

HttpHelper::HttpHelper(QObject *parent)
    : QObject(parent)
    ,networkManager(new QNetworkAccessManager(this))//初始化列表
{
     requestAdders[REGISTER]=QString(SERVER_URL)+"user/register";
     requestAdders[LOGIN]=QString(SERVER_URL)+"user/login";
     requestAdders[LOGINOUT]=QString(SERVER_URL)+"user/loginout";
     requestAdders[MEDIALIST]=QString(SERVER_URL)+"media/list";
     requestAdders[ADDMEDIA]=QString(SERVER_URL)+"media/add";
     requestAdders[EDITMEDIA]=QString(SERVER_URL)+"media/eidt";
     requestAdders[REMOVEMEDIA]=QString(SERVER_URL)+"media/remove";

     connect(networkManager,&QNetworkAccessManager::finished,this,&HttpHelper::onReplyFinished);


}
